import Vue from 'vue';
import Router from 'vue-router';
import initRoutes from './initRoutes';

Vue.use(Router);

const router = new Router({
    mode: 'history',
    routes: initRoutes
});

// 全局路由守卫，添加title
router.beforeEach((to, from, next) => {
    if(to.meta && to.meta.title){
        document.title = to.meta.title
    }
    next()
});

// 动态添加routes
const addAsyncRoutes = (router, routes) => {
    router.addRoutes(routes);
    // this.$router不是响应式的，所以手动将路由元注入路由对象
    router.options.routes = router.options.routes.concat(routes)
};

export {
    router,
    addAsyncRoutes
}